// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: XMLLoader.java

package com.market.web;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts.util.LabelValueBean;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.market.web.action.BaseAction;
import com.market.web.mail.MailInfo;
import com.market.web.utils.EncryptData;

public class XMLLoader
{

    static final Logger logger = LoggerFactory.getLogger(BaseAction.class);
    
    private XMLLoader()
    {
    }

    public static int getCount()
    {
        return getRootTextValue("count");
    }

    public static int getDefaultVipPriceRate()
    {
        return getRootTextValue("defaultVipPriceRate");
    }

    public static int getVipCoin()
    {
        return getRootTextValue("vipCoin");
    }

    private static int getRootTextValue(String name)
    {
        Node node = root.selectSingleNode(name);
        int n = 0;
        try
        {
            n = Integer.parseInt(node.getText());
        }
        catch (Exception e)
        {
            logger.error("",e);
        }
        return n;
    }

    public static boolean setCount(int value)
    {
        return setRootTextValue("count", value);
    }

    public static boolean setDefaultVipPriceRate(int value)
    {
        return setRootTextValue("defaultVipPriceRate", value);
    }

    public static boolean setVipCoin(int value)
    {
        return setRootTextValue("vipCoin", value);
    }

    public static int getCoinRate()
    {
        return getRootTextValue("coinRate");
    }

    public static boolean setCoinRate(int value)
    {
        return setRootTextValue("coinRate", value);
    }

    private static boolean setRootTextValue(String name, int n)
    {
        Node count = root.selectSingleNode(name);
        count.setText(String.valueOf(n));
        return save();
    }

    public static String getAdminName()
    {
        Node admin = root.selectSingleNode("admin");
        Node name = admin.selectSingleNode("name");
        return name.getText();
    }

    public static String getAdminPass()
    {
        Node admin = root.selectSingleNode("admin");
        Node pass = admin.selectSingleNode("password");
        return pass.getText();
    }

    public static boolean setAdminPass(String pass)
    {
        Node admin = root.selectSingleNode("admin");
        Node password = admin.selectSingleNode("password");
        password.setText(pass);
        return save();
    }

    public static boolean setAdminName(String name)
    {
        Node admin = root.selectSingleNode("admin");
        Node password = admin.selectSingleNode("name");
        password.setText(name);
        return save();
    }

    public List getUnit()
    {
        return getText("units", "unit");
    }

    private static boolean save()
    {
        try
        {
            OutputFormat format;
            FileOutputStream fos;
            format = OutputFormat.createPrettyPrint();
            fos = null;
            String file = url.getFile();
            fos = new FileOutputStream(new File(file.replaceAll("%20", " ")));
            XMLWriter writer = new XMLWriter(fos, format);
            writer.write(doc);
            writer.close();
            return true;
        }
        catch (Throwable e)
        {
            return false;
        }

    }

    public static ArrayList getBowaTypes()
    {
        return getTypes("bowa");
    }
    
    public static ArrayList getRiceTypes()
    {
        return getTypes("rice");
    }
    
    public static ArrayList getBeerTypes()
    {
        return getTypes("beer");
    }


    public static ArrayList getBowbTypes()
    {
        return getTypes("bowb");
    }

    public static ArrayList getBallTypes()
    {
        return getTypes("ball");
    }

    public static ArrayList getCollTypes()
    {
        return getTypes("collimator");
    }

    public static ArrayList getSafetyTypes()
    {
        return getTypes("safety");
    }

    public static ArrayList getArrowTypes()
    {
        return getTypes("arrow");
    }

    public static ArrayList getTypes(String category)
    {
        ArrayList list = new ArrayList();
        List l = root.selectSingleNode("types").selectSingleNode(category).selectNodes("type");
        for (int i = 0; i < l.size(); i++)
        {
            Element e = (Element)l.get(i);
            String name = e.getText();
            list.add(new LabelValueBean(name, name));
        }

        return list;
    }

    public static ArrayList getUnits()
    {
        ArrayList list = new ArrayList();
        List l = root.selectSingleNode("units").selectNodes("unit");
        for (int i = 0; i < l.size(); i++)
        {
            Element e = (Element)l.get(i);
            String name = e.getText();
            list.add(new LabelValueBean(name, name));
        }

        return list;
    }

    public static String getCallBoard()
    {
        return root.selectSingleNode("callBoard").getText();
    }

    public static boolean setCallBoard(String value)
    {
        Node node = root.selectSingleNode("callBoard");
        node.setText(value);
        return save();
    }

    private static ArrayList getText(String name, String elem)
    {
        ArrayList list = new ArrayList();
        Node node = root.selectSingleNode(name);
        List l = node.selectNodes(elem);
        for (int i = 0; i < l.size(); i++)
        {
            Node n = (Node)l.get(i);
            list.add(n.getText());
        }

        return list;
    }

    public static MailInfo getMailInfo()
    {
        MailInfo m = new MailInfo();
        Node node = root.selectSingleNode("mail");
        Node name = node.selectSingleNode("username");
        m.setUsername(name.getText());
        Node pass = node.selectSingleNode("password");
        m.setPassword(EncryptData.decrypt(pass.getText()));
        Node from = node.selectSingleNode("from");
        m.setFrom(from.getText());
        Node smtp = node.selectSingleNode("smtp");
        m.setSmtp(smtp.getText());
        return m;
    }

    public static boolean setMailInfo(MailInfo m)
    {
        Node node = root.selectSingleNode("mail");
        Node name = node.selectSingleNode("username");
        name.setText(m.getUsername());
        Node pass = node.selectSingleNode("password");
        pass.setText(EncryptData.encrypt(m.getPassword()));
        Node from = node.selectSingleNode("from");
        from.setText(m.getFrom());
        Node smtp = node.selectSingleNode("smtp");
        smtp.setText(m.getSmtp());
        return save();
    }

    public static boolean setUnits(String value)
    {
        String text[] = value.split(",");
        Element element = (Element)root.selectSingleNode("units");
        root.remove(element);
        Element e = root.addElement("units");
        for (int i = 0; i < text.length; i++)
        {
            Element el = e.addElement("unit");
            el.setText(text[i]);
            System.out.println("text:" + text[i]);
        }

        return save();
    }

    public static boolean setTypes(String category, String value)
    {
        String text[] = value.split(",");
        Element types = (Element)root.selectSingleNode("types");
        Element type = (Element)types.selectSingleNode(category);
        types.remove(type);
        Element ele = types.addElement(category);
        for (int i = 0; i < text.length; i++)
        {
            Element e = ele.addElement("type");
            e.setText(text[i]);
        }

        return save();
    }

    private static Document doc;

    private static Element root = null;

    static URL url;

    static
    {
        doc = null;
        ClassLoader classLoader = com.market.web.XMLLoader.class.getClassLoader();
        url = classLoader.getResource("market.xml");
        SAXReader reader = new SAXReader();
        try
        {
            doc = reader.read(url);
            root = doc.getRootElement();
        }
        catch (DocumentException e)
        {
            logger.error("",e);
        }
    }
}
